
package com.hyst.seviceImpl;

import com.hyst.domain.RecommendMsg;
import com.hyst.repository.RecommendMsgMapper;
import com.hyst.service.RecommendMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyst.domain.Recommend;
import com.hyst.domain.StudentView;
import com.hyst.repository.RecommendMapper;
import com.hyst.service.RecommendService;
import com.hyst.utils.UuidTool;

import java.util.List;

/**
 * @Description:
 * @Date：2018年09月26日 
 * @author： dy 
 * @version ：1.0 1.[2018年09月26日]
 */
@Service
public class RecommendServiceImpl implements RecommendService {

	@Autowired
	private RecommendMapper mapper;

	@Autowired
    private RecommendMsgMapper recommendMsgMapper;

	@Autowired
	private RecommendMsgService recommendService;


	@Override
	public Page<Recommend> list(final Recommend recommend) {
		final Page<Recommend> page = PageHelper.startPage(recommend.getPageNum(), recommend.getPageSize());
		mapper.select(recommend);

		List<Recommend> result = page.getResult();
		for(Recommend r:result){
			//总推荐次数
			Integer tuijianCount = mapper.getTuijianCount(r.getStudentId());
			r.setTotalAward(tuijianCount.toString());

			//已发放奖励次数
            Integer countJiang = recommendMsgMapper.getCountJiang(r.getStudentId());
            r.setGrantedAward(countJiang.toString());

            //待发放奖励次数
//			RecommendMsg recommendMsg = new RecommendMsg();
//			recommendMsg.setRecommenderId(r.getStudentId());
//			Integer count= recommendMsgMapper.select(recommendMsg).size();
//            r.setRemainedAward((count-countJiang)+"");

			final Integer noCountJiang = recommendMsgMapper.getNoCountJiang(r.getStudentId());
			r.setRemainedAward(noCountJiang+"");

			//推荐成功次数
			Integer successCount = mapper.getSuccessCount(r.getStudentId());
			r.setSuccessTimes(successCount.toString());
		}
		return page;
	}

	@Override
	public Recommend insert(final Recommend recommend) {
		recommend.setStudentId(UuidTool.randomUUID());
		mapper.insertSelective(recommend);
		return recommend;
	}

	@Override
	public Recommend getByPriKey(final String studentId) {
		final Recommend recommend = mapper.selectByPrimaryKey(studentId);
		return recommend;
	}

	@Override
	public Recommend updateSelective(final Recommend recommend) {
		mapper.updateByPrimaryKeySelective(recommend);
		return recommend;
	}

	@Override
	public Recommend update(final Recommend recommend) {
		mapper.updateByPrimaryKey(recommend);
		return recommend;
	}

	@Override
	public Recommend delete(final String studentId) {

		return null;
	}
	
	// 2018-9-26 chenbin: 根据负责人Id查询
	public Page<Recommend> getDataByFuzerenId(Recommend recommend, String fuzerenId) {
		final Page<Recommend> page = PageHelper.startPage(recommend.getPageNum(), recommend.getPageSize());
		if(fuzerenId==""){
			fuzerenId=null;
		}
		Recommend recmd = new Recommend();
		if (!fuzerenId.equals("all")){
			recmd.setFuzerenId(fuzerenId);
		} else {
			recmd.setFuzerenId("all");
		}
		mapper.getListFuzerenId(recmd);
		return page;
	}
	

}
